<template>
  <div class="goods-card" :style="containerStyle">
    <!-- 无商品 -->
    <div v-if="!data.goodsList || !data.goodsList.length" class="empty-container">
      <el-empty description="暂无商品" />
    </div>

    <!-- 商品卡片网格 -->
    <div
      v-else
      class="goods-grid"
      :style="gridStyle"
    >
      <div
        v-for="(goods, index) in data.goodsList"
        :key="index"
        class="goods-item"
        :style="itemStyle"
      >
        <div class="goods-image-container">
          <img
            :src="goods.image"
            :alt="goods.title"
            class="goods-image"
          />
        </div>

        <div class="goods-info">
          <h3 
            v-if="data.showTitle"
            class="goods-title"
          >{{ goods.title }}</h3>

          <div class="goods-price-container">
            <span 
              v-if="data.showPrice"
              class="goods-price"
            >¥{{ goods.price }}</span>

            <span 
              v-if="data.showOriginalPrice && goods.originalPrice"
              class="goods-original-price"
            >¥{{ goods.originalPrice }}</span>
          </div>

          <div 
            v-if="data.showSales && goods.sales"
            class="goods-sales"
          >已售{{ goods.sales }}件</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'

// 定义组件属性
const props = defineProps({
  data: {
    type: Object,
    default: () => ({})
  },
  styles: {
    type: Object,
    default: () => ({})
  }
})

// 计算容器样式
const containerStyle = computed(() => {
  return {
    marginTop: `${props.styles.marginTop || 0}px`,
    marginBottom: `${props.styles.marginBottom || 0}px`,
    marginLeft: `${props.styles.marginLeft || 0}px`,
    marginRight: `${props.styles.marginRight || 0}px`,
    backgroundColor: props.data.backgroundColor || '#ffffff'
  }
})

// 计算网格样式
const gridStyle = computed(() => {
  return {
    display: 'grid',
    gridTemplateColumns: `repeat(${props.data.columns || 2}, 1fr)`,
    gap: `${props.data.margin || 10}px`
  }
})

// 计算商品项样式
const itemStyle = computed(() => {
  return {
    borderRadius: `${props.data.borderRadius || 4}px`,
    overflow: 'hidden',
    backgroundColor: props.data.itemBackgroundColor || '#ffffff'
  }
})
</script>

<style lang="scss" scoped>
.goods-card {
  width: 100%;

  .empty-container {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 200px;
  }

  .goods-grid {
    width: 100%;

    .goods-item {
      display: flex;
      flex-direction: column;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
      transition: transform 0.3s;

      &:hover {
        transform: translateY(-5px);
      }

      .goods-image-container {
        width: 100%;
        padding-top: 100%;
        position: relative;
        overflow: hidden;

        .goods-image {
          position: absolute;
          top: 0;
          left: 0;
          width: 100%;
          height: 100%;
          object-fit: cover;
        }
      }

      .goods-info {
        padding: 8px;

        .goods-title {
          margin: 0 0 8px 0;
          font-size: 14px;
          color: #333;
          overflow: hidden;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-line-clamp: 2;
          -webkit-box-orient: vertical;
        }

        .goods-price-container {
          display: flex;
          align-items: center;
          margin-bottom: 5px;

          .goods-price {
            font-size: 16px;
            font-weight: bold;
            color: #f44;
            margin-right: 5px;
          }

          .goods-original-price {
            font-size: 12px;
            color: #999;
            text-decoration: line-through;
          }
        }

        .goods-sales {
          font-size: 12px;
          color: #999;
        }
      }
    }
  }
}
</style>
